package zgrjb.snbq.teach_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zgrjb.snbq.teach_platform.dto.model.FileDO;
import zgrjb.snbq.teach_platform.dto.model.User;
import zgrjb.snbq.teach_platform.dto.req.AddFileRequest;
import zgrjb.snbq.teach_platform.interceptor.LoginInterceptor;
import zgrjb.snbq.teach_platform.mapper.FileMapper;
import zgrjb.snbq.teach_platform.service.FileService;
import zgrjb.snbq.teach_platform.util.IDUtil;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.util.List;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileMapper fileMapper;

    @Override
    public JsonData addAddFile(AddFileRequest request) {
        // 获取当前登录用户信息
        User currentUser = LoginInterceptor.getCurrentUser();
        if (currentUser == null) {
            return JsonData.buildError("用户未登录");
        }

        FileDO fileDO = FileDO.builder()
                .id((Long) IDUtil.geneSnowFlakeID())
                .name(request.getName())
                .description(request.getDescription())
                .type(request.getType())
                .url(request.getUrl())
                .classId(request.getClassId())
                .courseId(request.getCourseId())
                .uploaderId(currentUser.getId())
                .uploaderType(currentUser.getType())
                .fileSize(request.getFileSize() != null ? request.getFileSize() : 0L)
                .downloadCount(0)
                .del(0)
                .publish(0)
                .isPublic(request.getIsPublic() != null ? request.getIsPublic() : 1)
                .status(1)
                .build();
        
        fileMapper.insert(fileDO);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData list(Long classId) {
        List<FileDO> list = fileMapper.selectList(new QueryWrapper<FileDO>()
                .eq("class_id", classId)
                .eq("del", 0)
                .eq("publish", 1)
                .eq("status", 1));
        return JsonData.buildSuccess(list);
    }

    @Override
    public JsonData listAll(Long classId) {
        List<FileDO> list = fileMapper.selectList(new QueryWrapper<FileDO>()
                .eq("class_id", classId)
                .eq("del", 0)
                .eq("status", 1));
        return JsonData.buildSuccess(list);
    }

    @Override
    public JsonData publishFile(Long fileId) {
        fileMapper.update(new UpdateWrapper<FileDO>()
                .eq("id", fileId)
                .set("publish", 1));
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData delFile(Long fileId) {
        fileMapper.update(new UpdateWrapper<FileDO>()
                .eq("id", fileId)
                .set("del", 1));
        return JsonData.buildSuccess();
    }
}
